package xyz.bali16.module.invitecode.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.bali16.application.core.utils.TimeUtil;
import xyz.bali16.module.invitecode.entity.Invitation;
import xyz.bali16.module.invitecode.entity.InviteCode;
import xyz.bali16.module.invitecode.mapper.InvitationMapper;
import xyz.bali16.module.invitecode.model.invitation.InvitationAddParam;
import xyz.bali16.module.invitecode.model.invitation.InvitationPageParam;
import xyz.bali16.module.invitecode.model.invitation.InvitationUpdateParam;
import xyz.bali16.module.invitecode.service.InvitationService;
import xyz.bali16.module.invitecode.service.InviteCodeService;


/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@Service
public class InvitationServiceImpl extends ServiceImpl<InvitationMapper, Invitation> implements InvitationService {
    public InviteCodeService inviteCodeService;



    @Override
    public IPage<Invitation> getPage(InvitationPageParam param) {
        IPage<?> page = new Page<>(param.getCurrent(), param.getSize());
        return this.baseMapper.getPage(page, param.getInviterId(),param.getInviteeId(),param.getInvitationCode());
    }

    @Override
    public void addInvitation(InvitationAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "InvitationAddParam 参数不能为为空");
        Invitation invitation = new Invitation();
        invitation.setGmtCreated(TimeUtil.getNowTimeToSeconds());
        BeanUtils.copyProperties(param, invitation);
        Wrapper<InviteCode> wrapper = Wrappers.<InviteCode>lambdaQuery().eq(InviteCode::getCode,invitation.getInvitationCode());
        InviteCode inviteCode = inviteCodeService.getOne(wrapper);
        Integer restNum = inviteCode.getNum();
        if(restNum>0||inviteCode.getIsUsed()==0){
            inviteCode.setNum(restNum-1);
            if(inviteCode.getNum()<0||inviteCode.getNum()==0){
                inviteCode.setIsUsed(1);
            }
        }else{
            return;
        }
        inviteCodeService.updateById(inviteCode);
        System.out.println(invitation);
        save(invitation);
    }

    @Override
    public void updateInvitation(InvitationUpdateParam param) throws Exception {
        Preconditions.checkNotNull(param, "InvitationUpdateParam 参数不能为为空");
        Invitation invitation = new Invitation();
        BeanUtils.copyProperties(param, invitation);
        updateById(invitation);
    }


    @Override
    public void delete(String invitationId) throws Exception {
        if (invitationId == null) {
            return;
        }
        removeById(invitationId);

    }
}
